package com.fernfx.xingtan.contacts;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseAdapter;
import com.fernfx.xingtan.contacts.contract.NewFriendsContract;
import com.fernfx.xingtan.contacts.entity.NewFriendsEntity;
import com.fernfx.xingtan.utils.GlideUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.fernfx.xingtan.utils.UIUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/8/7.
 */

public class NewFriendsAdapter extends BaseAdapter {
    private List<NewFriendsEntity.ObjBean.UserRelationVOListBean> userRelationVOList;
    private Context context;
    private final static Map<Integer,StatusEntity> STATUS_MAP = initStatusMap();

    //current adapter
    private NewFriendsAdapter newFriendsAdapter = this;
    //presenter
    private NewFriendsContract.Presenter presenter;

    public NewFriendsAdapter(Context context, List datas) {
        super(context, datas);
        this.userRelationVOList = datas;
        this.context = context;
    }

    public void setNewFriendsPresenter(NewFriendsContract.Presenter presenter){
        this.presenter = presenter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_new_friends_item;
    }

    @Override
    public Holder initHolder(View container) {
        NewFriendsHolder newFriendsHolder = new NewFriendsHolder();
        //init view
        newFriendsHolder.friendsHeadPortraitIv = container.findViewById(R.id.friends_head_portrait_iv);
        newFriendsHolder.friendsNodeTv = container.findViewById(R.id.friends_node_tv);
        newFriendsHolder.friendsRemarksTv = container.findViewById(R.id.friends_remarks_tv);
        newFriendsHolder.addFriendsStatusTv = container.findViewById(R.id.add_friends_status_tv);
        return newFriendsHolder;
    }

    @Override
    public void initItem(View view, Holder holder, int position) {
        final NewFriendsEntity.ObjBean.UserRelationVOListBean userRelationVOListBean = userRelationVOList.get(position);
        //view bean
        NewFriendsHolder newFriendsHolder = (NewFriendsHolder)holder;
        if (null == userRelationVOListBean) {
            return;
        }
        String headImg = userRelationVOListBean.getHeadImg();
        //check headImg
        headImg = TextUtils.isEmpty(headImg) ? "" : headImg;
        GlideUtil.loadImage(context,headImg,newFriendsHolder.friendsHeadPortraitIv);
        //昵称
        String nickName = userRelationVOListBean.getNickname();
        nickName = TextUtils.isEmpty(nickName) ? "" : nickName;
        newFriendsHolder.friendsNodeTv.setText(nickName);
        //备注
        String message = userRelationVOListBean.getMessage();
        message = TextUtils.isEmpty(message) ? "" : message;
        newFriendsHolder.friendsRemarksTv.setText(message);
        //状态
        int status = userRelationVOListBean.getStatus();
        if (STATUS_MAP.containsKey(status)) {
            final StatusEntity statusEntity = STATUS_MAP.get(status);
            //设置颜色或内容
            newFriendsHolder.addFriendsStatusTv.setTextColor(UIUtil.getColor(statusEntity.colorStatus));
            newFriendsHolder.addFriendsStatusTv.setText(statusEntity.statusText);
            if (statusEntity.clickFlag) {
                newFriendsHolder.addFriendsStatusTv.setOnClickListener(null);
                newFriendsHolder.addFriendsStatusTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String friendsId = userRelationVOListBean.getFriendId();
                        if (TextUtils.isEmpty(friendsId)) {
                            ToastUtil.showCentertoast("好友账号为空");
                            return;
                        }
                        //init request Map
                        Map<String,Object> requestArgsMap = new HashMap<>(2);
                        requestArgsMap.put("friendId",friendsId);
                        requestArgsMap.put("operate","1");
                        presenter.operationAddFriends(newFriendsAdapter,userRelationVOListBean,requestArgsMap,statusEntity);
                    }
                });
            }
        }
    }

    public void addFriendsSucess(NewFriendsEntity.ObjBean.UserRelationVOListBean userRelationVOListBean,NewFriendsAdapter.StatusEntity statusEntity){
        userRelationVOListBean.setStatus(1);
        statusEntity.clickFlag = false;
        //刷新adapter
        notifyDataSetInvalidated();
        notifyDataSetChanged();
    }

    public final class NewFriendsHolder extends Holder{
        ImageView friendsHeadPortraitIv;

        TextView friendsNodeTv;

        TextView friendsRemarksTv;

        TextView addFriendsStatusTv;
    }

    private final static Map<Integer,StatusEntity> initStatusMap(){
        //1,已添加好友;2,请求添加;3,请求被添加;4,请求被拒绝;5,我被对方删除
        Map<Integer,StatusEntity> map = new HashMap<>(3);
        final StatusEntity addedEntity = new StatusEntity();
        addedEntity.statusText = "已添加";
        addedEntity.clickFlag = false;
        addedEntity.colorStatus = R.color.gray;
        map.put(1,addedEntity);

        final StatusEntity otherRequestAddEntity = new StatusEntity();
        otherRequestAddEntity.statusText = "待添加";
        otherRequestAddEntity.clickFlag = true;
        otherRequestAddEntity.colorStatus = R.color.green;
        map.put(3,otherRequestAddEntity);
        return map;
    }

    public static class StatusEntity {
        public String statusText;

        public int colorStatus;

        public boolean clickFlag = false;
    }
}
